﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AffinityORMLibrary.DataTypes;
using AffinityORMLibrary.DataLayer.Utility;

namespace AffinityORMLibrary.DataLayer.Assembler
{
    class SysTableColumnAssembler : IAssembler<SysTableColumn>
    {
        public SysTableColumn Construct(System.Data.IDataReader reader)
        {
            SysTableColumn result = new SysTableColumn();

            int tableNameIndex = reader.GetOrdinal("TABLE_NAME");
            if (!reader.IsDBNull(tableNameIndex))
            {
                result.TABLE_NAME = reader.GetString(tableNameIndex);
                result.UpdateOldKey("TABLE_NAME", result.TABLE_NAME);
            }

            int columnNameIndex = reader.GetOrdinal("COLUMN_NAME");
            if (!reader.IsDBNull(columnNameIndex))
            {
                result.COLUMN_NAME = reader.GetString(columnNameIndex);
                result.UpdateOldKey("COLUMN_NAME", result.COLUMN_NAME);
            }

            int isNullableIndex = reader.GetOrdinal("IS_NULLABLE");
            if (!reader.IsDBNull(isNullableIndex))
            {
                result.IS_NULLABLE = reader.GetString(isNullableIndex) == "No" ? false : true;
                result.UpdateOldKey("IS_NULLABLE", result.IS_NULLABLE);
            }

            int originalPositionIndex = reader.GetOrdinal("ORDINAL_POSITION");
            if (!reader.IsDBNull(originalPositionIndex))
            {
                result.ORDINAL_POSITION = Convert.ToInt32(reader.GetValue(originalPositionIndex));
                result.UpdateOldKey("ORDINAL_POSITION", result.ORDINAL_POSITION);
            }

            int columnDefaultIndex = reader.GetOrdinal("COLUMN_DEFAULT");
            if (!reader.IsDBNull(columnDefaultIndex))
            {
                result.COLUMN_DEFAULT = reader.GetString(columnDefaultIndex);
                result.UpdateOldKey("COLUMN_DEFAULT", result.COLUMN_DEFAULT);
            }

            int dataTypeIndex = reader.GetOrdinal("DATA_TYPE");
            if (!reader.IsDBNull(dataTypeIndex))
            {
                result.DATA_TYPE = SQLDataTypeMap.MapSqlDataType(reader.GetString(dataTypeIndex));
                result.UpdateOldKey("DATA_TYPE", result.DATA_TYPE);
            }

            int characterMaxLengthIndex = reader.GetOrdinal("CHARACTER_MAXIMUM_LENGTH");
            if (!reader.IsDBNull(characterMaxLengthIndex))
            {
                result.CHARACTER_MAXIMUM_LENGTH = Convert.ToInt32(reader.GetValue(characterMaxLengthIndex));
                result.UpdateOldKey("CHARACTER_MAXIMUM_LENGTH", result.CHARACTER_MAXIMUM_LENGTH);
            }

            int numericPrecisionIndex = reader.GetOrdinal("NUMERIC_PRECISION");
            if (!reader.IsDBNull(numericPrecisionIndex))
            {
                result.NUMERIC_PRECISION = Convert.ToInt32(reader.GetValue(numericPrecisionIndex));
                result.UpdateOldKey("NUMERIC_PRECISION", result.NUMERIC_PRECISION);
            }

            int datetimePrecisionIndex = reader.GetOrdinal("DATETIME_PRECISION");
            if (!reader.IsDBNull(datetimePrecisionIndex))
            {
                result.DATETIME_PRECISION = Convert.ToInt32(reader.GetValue(datetimePrecisionIndex));
                result.UpdateOldKey("DATETIME_PRECISION", result.DATETIME_PRECISION);
            }

            return result;
        }
    }
}
